package com.cmsky.open.shop.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.org.rapid_framework.web.scope.Flash;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.cmsky.open.base.manager.AreaInfoManager;
import com.cmsky.open.base.manager.GoodsClassifyInfoManager;
import com.cmsky.open.core.base.BasePager;
import com.cmsky.open.core.util.JsonUtils;
import com.cmsky.open.core.util.PagerUitl;

import cn.org.rapid_framework.beanutils.BeanUtils;

import java.util.*;

import com.cmsky.open.core.base.*;
import com.cmsky.open.core.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import com.cmsky.open.shop.model.*;
import com.cmsky.open.shop.dao.*;
import com.cmsky.open.shop.manager.*;
import com.cmsky.open.shop.vo.query.*;

/**
 * @author jandyfish email:jandyfish(a)gmail.com
 * @version 1.0
 * @since 1.0
 */


@Controller("admin.shop.shopAreaInfo")
@RequestMapping("/admin/shop/shopAreaInfo")
public class ShopAreaInfoController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private ShopAreaInfoManager shopAreaInfoManager;
	private ShopInfoManager shopInfoManager;
	private AreaInfoManager areaInfoManager;
	private GoodsClassifyInfoManager goodsClassifyInfoManager;
	private final String LIST_ACTION = "redirect:/admin/shop/shopAreaInfo/list.do";
	
	public ShopAreaInfoController() {
	}
	
	/** 
	 * 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写
	 **/
	public void setShopAreaInfoManager(ShopAreaInfoManager manager) {
		this.shopAreaInfoManager = manager;
	}

	public void setShopInfoManager(ShopInfoManager shopInfoManager) {
		this.shopInfoManager = shopInfoManager;
	}

	public void setAreaInfoManager(AreaInfoManager areaInfoManager) {
		this.areaInfoManager = areaInfoManager;
	}

	public void setGoodsClassifyInfoManager(
			GoodsClassifyInfoManager goodsClassifyInfoManager) {
		this.goodsClassifyInfoManager = goodsClassifyInfoManager;
	}

	/**
	 * 增加了@ModelAttribute的方法可以在本controller的方法调用前执行,可以存放一些共享变量,如枚举值
	 */
	@ModelAttribute
	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
	
	/** 
	 * 执行搜索 
	 **/
	@RequestMapping("list")
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,ShopAreaInfoQuery query) {
		BasePager page = this.shopAreaInfoManager.findPage(query);
		
		ModelAndView result = new ModelAndView("/admin/shop/shopAreaInfo/list");
		result.addAllObjects(toModelMap(page, query));
		return result;
	}
	
	/**
	 * 默认管理页
	 */
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response){
		return new ModelAndView("/admin/shop/shopAreaInfo/index");
	}
	/**
	 * ajax搜索
	 * @param request
	 * @param response
	 * @param query
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/ajaxList")
	public String ajaxList(HttpServletRequest request,HttpServletResponse response,ShopAreaInfoQuery query) {
		BasePager<ShopAreaInfo> page = this.shopAreaInfoManager.findPage(query);
		if(null!=page.getRows()){
			for(ShopAreaInfo sa : page.getRows()){
				sa.setGoodsClassifyInfo(this.goodsClassifyInfoManager.getById(sa.getGoodsClassifyId()));
				sa.setAreaInfo(this.areaInfoManager.getById(sa.getAreaId()));
				sa.setShopInfo(this.shopInfoManager.getById(sa.getShopId()));
			}
		}
		return JsonUtils.jsonFromObject(PagerUitl.pasreToLigerMap(page));
	}
	/**
	 * 批量更新或新增
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(HttpServletRequest request,HttpServletResponse response){
		String json = request.getParameter("json");
		Map<String,Object> result = new HashMap<String,Object>();
		List<Integer> successId = new LinkedList<Integer>();
		List tempList = JsonUtils.listFromJson(json);
		for(Object obj : tempList){
			ShopAreaInfo bean = JsonUtils.objectFromJson(JsonUtils.jsonFromObject(obj),ShopAreaInfo.class);
			if(this.shopAreaInfoManager.saveOrUpdate(bean) > 0){
				successId.add(bean.getId());
			}
		}
		result.put("sucessList", successId);
		return JsonUtils.jsonFromObject(result);
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping("show")
	public ModelAndView show(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.Integer id = new java.lang.Integer(request.getParameter("id"));
		ShopAreaInfo shopAreaInfo = (ShopAreaInfo)shopAreaInfoManager.getById(id);
		return new ModelAndView("/admin/shop/shopAreaInfo/show","shopAreaInfo",shopAreaInfo);
	}
	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping("create")
	public ModelAndView create(HttpServletRequest request,HttpServletResponse response,ShopAreaInfo shopAreaInfo) throws Exception {
		return new ModelAndView("/admin/shop/shopAreaInfo/create","shopAreaInfo",shopAreaInfo);
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping("save")
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response,ShopAreaInfo shopAreaInfo) throws Exception {
		shopAreaInfoManager.save(shopAreaInfo);
		Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return new ModelAndView(LIST_ACTION);
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping("edit")
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.Integer id = new java.lang.Integer(request.getParameter("id"));
		ShopAreaInfo shopAreaInfo = (ShopAreaInfo)shopAreaInfoManager.getById(id);
		return new ModelAndView("/admin/shop/shopAreaInfo/edit","shopAreaInfo",shopAreaInfo);
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping("update")
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.Integer id = new java.lang.Integer(request.getParameter("id"));
		
		ShopAreaInfo shopAreaInfo = (ShopAreaInfo)shopAreaInfoManager.getById(id);
		bind(request,shopAreaInfo);
		shopAreaInfoManager.update(shopAreaInfo);
		Flash.current().success(UPDATE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
	/**
	 *删除对象
	 **/
	@RequestMapping("delete")
	public ModelAndView delete(HttpServletRequest request,HttpServletResponse response) {
		String[] items = request.getParameterValues("items");
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			
			java.lang.Integer id = new java.lang.Integer((String)params.get("id"));
			
			shopAreaInfoManager.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
}

